import gugga.animations.TimeLineAnimation;
import gugga.application.Section;
import gugga.common.ITask;
import gugga.sequence.ExecuteMethodTask;
import gugga.sequence.TaskSequence;

/**
 * @author bony
 * The class CommonSection usually content all assets,
 * that are common for more the sections in some application.
 */ 
class useOfSequences.CommonSection extends Section 
{
	// List the CommonSection fields	
	// TimeLineAnimation is mode to encapsulate the Flash authoring environment animation in task.
	// The task start and complete when the animation do it.  
	private var mOpenAnimation:TimeLineAnimation;
	private var mCloseAnimation:TimeLineAnimation;	
	
	// Those five animations are equal for all sections in the application.	
	private var mTitleAnim: TimeLineAnimation;
	private var mMailAnim: TimeLineAnimation;
	private var mLogoAnim: TimeLineAnimation;
	private var mCupAnim: TimeLineAnimation;
	private var mEggsAnim: TimeLineAnimation;	
	
	public function CommonSection() 
	{
		super();
		
		// Get the instances from the scene.
		mOpenAnimation = getComponent("OpenSection");
		mCloseAnimation = getComponent("CloseSection");
			
		mTitleAnim = getComponent("TitleAnim");
		mMailAnim = getComponent("MailAnim");
		mLogoAnim = getComponent("LogoAnim");
		mCupAnim = getComponent("CupAnim");
		mEggsAnim = getComponent("EggsAnim");
		
	}
	
	public function initUI ()
	{
		super.initUI();
	
		// Prepare and register section sequences.
		 		
		// Initialization sequence
		// Using default initialization sequence. 
		
		// Activating sequence
		// Using default activating sequence. 
		
		// Open sequence
		// Show section and start open animation 
		// Some section could have different appearance.    
		var openSequence:TaskSequence = new TaskSequence();
		openSequence = prepareOpeningSequence(); 
		this.registerOpeningTask(openSequence);
		
		// Close sequence
		// Close animation and hide section -  this is behavior we like this section to have 
		// some section could have different appearance.     
		var closeSequence:TaskSequence = new TaskSequence();
		closeSequence = prepareClosingSequence();
		this.registerClosingTask(closeSequence);
	}
	
	private function prepareOpeningSequence() : TaskSequence
	{
		// Create task that will show current section - 
		// this task will be included in appropriate place in open sequence. 
		var showTask:ITask = ExecuteMethodTask.create(this, this.show, null);
		
		var openSequence:TaskSequence = new TaskSequence();
		openSequence.addTask(showTask);
		openSequence.addTask(mOpenAnimation);
		
		return openSequence;
	}

	private function prepareClosingSequence() : TaskSequence
	{				
		// Create task that will hide current section - 
		// this task will be included in appropriate place in close sequence. 
		var hideTask:ITask = ExecuteMethodTask.create(this, this.hide, null);
		
		var closeSequence:TaskSequence = new TaskSequence();
		closeSequence.addTask(mCloseAnimation);
		closeSequence.addTask(hideTask);
		
		return closeSequence;
	}
}